package com.sso.manager;

import com.sso.common.ConstantDate;
import com.sso.dao.SysUserRoleMapper;
import com.sso.entity.SysRole;
import com.sso.entity.SysUser;
import com.sso.entity.SysUserRole;
import com.sso.exception.DataBaseException;
import com.sso.exception.SelectException;
import com.tools.util.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysUserRoleManager implements BaseManager<SysUserRole> {
    @Autowired
    private SysUserRoleMapper userRoleDao;

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int save(SysUserRole entity) throws Exception {
        if(StrUtils.isEmpty(entity.getId())){
            entity.setId(ConstantDate.getUUID());
            userRoleDao.insert(entity);
        }else{
            userRoleDao.update(entity);
        }
        return 0;
    }

    @Override
    @Transactional(propagation=Propagation.SUPPORTS)
    public List<SysUserRole> selectByPage(Map<String, Object> param) {
        return null;
    }

    @Override
    @Transactional(propagation=Propagation.SUPPORTS)
    public List<SysUserRole> selectByProperty(Map<String, Object> param) {
        return userRoleDao.selectByProperty(param);
    }

    @Transactional(propagation=Propagation.SUPPORTS)
    public List<SysUserRole> selectByUserId(String userId) {
        Map<String,Object> param = new HashMap<String, Object>();
        param.put("userId",userId);
        return this.selectByProperty(param);
    }

    @Override
    @Transactional(propagation= Propagation.SUPPORTS)
    public long countByProperty(Map<String, Object> param){
        return 0;
    }

    @Transactional(rollbackFor = Exception.class)
    public int deleteByUserId(String userid) throws Exception {
        Map<String,Object> filterMap = new HashMap<String, Object>();
        filterMap.put("userId",userid);
        return userRoleDao.delete(filterMap);
    }

    @Transactional(rollbackFor = Exception.class)
    public int deleteByRoleId(String roleId) throws Exception {
        Map<String,Object> filterMap = new HashMap<String, Object>();
        filterMap.put("roleId",roleId);
        return userRoleDao.delete(filterMap);
    }
}
